<?php
include_once 'db.php';
session_start();
if(!$_SESSION["passed"]) return false;

$returnObj = array("code"=>"0", "desc" => "新增评价失败,缺少必填属性");

$actionType = $_POST["atype"];
$param = $_POST["param"];
// var_dump($param);
// $param = json_decode($queryParam, true);
// echo $queryParam;
// $m_code = $_POST["mid"]; // mission_code任务主键
// $m_name = $_POST["missionName"]; // 任务名称
// $m_status = $_POST["missionStatus"]; // 任务状态
// $m_end_date = $_POST["endDate"]; // 结束日期
// $m_desc = $_POST["desc"]; // 任务简介
// $m_employee_num = $_POST["employerNum"]; // 待寻人员类别及数量
// $user_code = $_POST["userCode"]; // 任务创建人id



/**
 * [exist_comment 判断评价记录是否存在,删除或更新前应该先执行一次]
 * @param  [String]  $c_code [评价id]
 * @return [Boolean]         [true-存在;false-不存在]
 */
function exist_comment($c_code) {
    $count = 0;
    if(!$m_code) return false;
    $query_str = "select count(*) as count from l_comment where c_code =  %q";
    $query_param = array($c_code);
    $count = mysql_fetch_assoc(dbOperate($query_str, $query_param));
    $count = $count['count'];
    return $count > 0 ? true : false;
}
/**
 * [get_comment_num description]
 * @param  [String]  $query_where [查询where语句]
 * @param  [Object]  $query_param [查询条件参数]
 * @return [Integer]              [1-成功;0-失败]
 */
function get_comment_num($query_where, $query_param) {
    $query_str = "select count(*) as count from l_comment where 1 = 1 ".$query_where;
    // echo $query_str;
    $count = mysql_fetch_assoc(dbOperate($query_str, $query_param));
    $count = $count['count'];
    return $count ? $count : 0;
}

function insert_comment($param) {
    $result = 0;
    $c_origin_code = $param['c_origin_code'];
    $c_origin_name = $param['c_origin_name'];
    $c_target_code = $param['c_target_code'];
    $c_target_name = $param['c_target_name'];
    $c_content = $param['c_content'];

    if(!$c_origin_code || !$c_origin_name || !$c_target_code || !$c_target_name || !$c_content) return $result;

    $query_str = "insert into l_comment (id, c_code, c_origin_code, c_origin_name, c_target_code, c_target_name,"
        ." c_content, last_modify) values ('', '".uniqid()."', %q, %q, %q, %q, %q, now())";
    $query_param = array($c_origin_code, $c_origin_name, $c_target_code, $c_target_name, $c_content);

    $result = dbOperate($query_str, $query_param);
    return $result;
}
/**
 * [delete_comment 删除任务]
 * @param  [String] $c_code [评价id]
 * @return [Int]            [mysql返回值,1:成功,0:失败]
 */
function delete_comment($param) {
    $result = 0;
    $c_code = $param['c_code'];
    if(!$c_code) return $result;
    $query_str = "delete from l_comment where c_code = %q";
    $query_param = array($m_code);

    $result = dbOperate($query_str, $query_param);
    return $result;
}

function update_comment($param) {
    $result = 0;
    $c_code = $param['c_code'];
    $c_origin_code = $param['c_origin_code'];
    $c_origin_name = $param['c_origin_name'];
    $c_target_code = $param['c_target_code'];
    $c_target_name = $param['c_target_name'];
    $c_content = $param['c_content'];
    $clip = array();
    $query_param = array();
    // if(!$m_name || !$m_end_date || !$m_employee_num || !$user_code) return $result;

    if($c_origin_code) {
        array_push($clip, 'c_origin_code = %q, ');
        array_push($query_param, $c_origin_code);
    }
    if($c_origin_name) {
        array_push($clip, 'c_origin_name = %q, ');
        array_push($query_param, $c_origin_name);
    }
    if($c_target_code) {
        array_push($clip, 'c_target_code = %q, ');
        array_push($query_param, $c_target_code);
    }
    if($c_target_name) {
        array_push($clip, 'c_target_name = %q, ');
        array_push($query_param, $c_target_name);
    }
    if($c_content) {
        array_push($clip, 'c_content = %q, ');
        array_push($query_param, $c_content);
    }

    array_push($query_param, $m_code);
    $query_str = "update l_comment set ".join('', $clip)."last_modify = now() where c_code = %q";
    // $query_str = "update l_mission set m_name = %q, m_status = %i, m_end_date = %q, m_desc = %q,"
    //     ." m_employee_num = %q, m_employees = %q, user_code = %q, last_modify = now() where m_code = %q";
    // $query_param = array($m_name, $m_status, $m_end_date, $m_desc, $m_employee_num, $m_employees, $user_code, $m_code);

    $result = dbOperate($query_str, $query_param);
    return $result;
}

function query_comment($param) {
    $result = 0;

    // var_dump($param);
    // echo $param['user_code'].",".$param['user_name']."  ";
    $user_type = $param['userType'];
    $user_code = $param['userCode'];
    $user_name = $param['userName'];
    $page = $param['page'];
    $page_size = $param['pageSize'];
    $sortName = $param['sortName'] ? $param['sortName'] : 'last_modify';
    $sortOrder = $param['sortOrder'] ? $param['sortOrder'] : 'desc';

    $query_param = array();
    $clip = "";
    if(!$user_code || !$user_name) return $result;
    if(!!$user_code) {
        $clip = $clip." and c_origin_code = %q ";
        array_push($query_param, $user_code);
    }
    if(!!$user_name) {
        $clip = $clip." and instr('c_origin_name', '%s') > -1 ";
        array_push($query_param, $user_name);
    }
    // echo $user_code.",".$user_name."  ";
    $count = get_comment_num($clip, $query_param);
    $pages = intval($count / $page_size);
    $pages = $count % $page_size > 0 ? $pages + 1 : $pages;

    array_push($query_param, $page - 1);
    array_push($query_param, $page_size);
    // var_dump($query_param);
    $query_str = "select l.* from l_comment l where 1 = 1 ".$clip." order by ".$sortName." ".$sortOrder." limit %i, %i";
    // echo $query_str;
    $result = dbOperate($query_str, $query_param);

    $arr = array();
    while($row = mysql_fetch_assoc($result))
    {
      array_push($arr, $row);
    }
    $rows = array("Rows"=>$arr, "curPage"=>$page, "pages"=>$pages);

    return json_encode($rows);
}

$result = 0;
switch($actionType) {
    case "c":
        $result = insert_comment($param);
        break;
    case "r":
        $result = query_comment($param);
        break;
    case "u":
        $result = update_comment($param);
        break;
    case "d":
        $result = delete_comment($param);
        break;
    default:
        $result = 0;
        break;
}
echo $result;

?>